package com.atguigu.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.MemberFeignService;
import com.atguigu.gulimall.ware.vo.FareVo;
import com.atguigu.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareInfoDao;
import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {


    @Autowired
    MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {

        FareVo fareVo = new FareVo();
        // 获取地址信息
        R info = memberFeignService.info(addrId);
        if (info != null) {
            MemberAddressVo vo = (MemberAddressVo) info.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>(){});
            String phone = vo.getPhone();
            Random random = new Random();
            String yunFei = "";
            // 随机获取两位手机号，当运费。  （ 可链接第三方物流接口去调试）

            yunFei = phone.substring(9);
//            for (int i = 0; i < 2; i++) {
//                int index = random.nextInt(phone.length());
//                char c = phone.charAt(index);
//                yunFei = yunFei + c;
//            }
            BigDecimal bigDecimal = new BigDecimal(yunFei);

            fareVo.setFare(bigDecimal);
            fareVo.setAddress(vo);

        }
        return fareVo;
    }

}